/**log module of frame work, it works for exception message and also can record user's operation behavour
 * by fuzhou huilin corp.ltd @author lms 2016.07.31
 */

package com.fzrskj.cloud.free.sys.dao;

import com.fzrskj.cloud.free.core.tool.CDateTimeTool;
import com.fzrskj.cloud.free.core.utils.CloudContextUtils;
import com.fzrskj.cloud.free.core.utils.CloudDbDrvUtils;
import com.fzrskj.cloud.free.sys.bean.RuntimeLog;
import ms.core.consts.SysErr;
import ms.core.db.IDbDrv;
import ms.core.object.CObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CLogDao extends CObject{
	private IDbDrv db;

	public CLogDao(){
		this.db = CloudDbDrvUtils.getDbUtilsDrv();
	}

	@Override
	public void dispose(){
		this.db.closeConnection();
	}

	private void logRuntimeError(String module, String msg) {
		String sql = "insert into sys_runtimelog(logDesc,logTime,logTypeId,logTypeName,module,agentId) values(?,?,?,?,?,?);";
		Object[] params = {msg, new Date(), 1, "系统异常日志", module, CloudContextUtils.getAgentId()};
		this.db.executeSql(sql, params);
	}

	public int addLogRuntimeError(String moduleName, int logTypeId, String logTypeName, String logDesc, int uid, String uName) {
		String sql = "insert into sys_runtimelog(logDesc,logTime,logTypeId,logTypeName,module," +
				"userId,userName,agentId) values(?,?,?,?,?," +
				"?,?,?);";
		Object[] params = {logDesc, new Date(), logTypeId, logTypeName, moduleName,
				uid, uName, CloudContextUtils.getAgentId() == null ? CloudContextUtils.DEF_AGENT_ID : CloudContextUtils.getAgentId()};
		int ret = db.executeSql(sql, params);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	/**
	 * to query runtime log count
	 *
	 * @param fromTime the logtime format is yyyyMMddhhmmss
	 * @param toTime   the logtime format is yyyyMMddhhmmss
	 * @return runtime log count
	 * @author lms 2016.08.06
	 */
	public int queryRuntimeLog(long fromTime, long toTime) {
		Date df = CDateTimeTool.intToDateTime(fromTime);
		Date dt = CDateTimeTool.intToDateTime(toTime);
		List<Object> tmp = new ArrayList<>();

		String sql = "select count(id) from sys_runtimelog where 1>0 and agentId=?";
		tmp.add(CloudContextUtils.getAgentId());

		if (df != null) {
			sql = sql + " and logTime>=?";
			tmp.add(df);
		}
		if (dt != null) {
			sql = sql + " and logTime<=?";
			tmp.add(dt);
		}
		Object[] params = new Object[tmp.size()];
		for (int i = 0; i < tmp.size(); i++) {
			params[i] = tmp.get(i);
		}

		Long v = (Long) this.db.queryFieldValue(sql, Long.class, params, 1);
		if (v == null) {
			return 0;
		}
		return v.intValue();
	}

	/**
	 * to query runtime log
	 *
	 * @param fromTime the logtime format is yyyyMMddhhmmss
	 * @param toTime   the logtime format is yyyyMMddhhmmss
	 * @param page     page index, must start from 1
	 * @param pageSize pageSize must over then 1
	 * @return null or RuntimeLog list
	 */
	public List<RuntimeLog> queryRuntimeLog(long fromTime, long toTime, int page, int pageSize) {
		Date df = CDateTimeTool.intToDateTime(fromTime);
		Date dt = CDateTimeTool.intToDateTime(toTime);
		List<Object> tmp = new ArrayList<>();

		String sql = "select * from sys_runtimelog where 1>0 and agentId=?";

		tmp.add(CloudContextUtils.getAgentId());

		if (df != null) {
			sql = sql + " and logTime>=?";
			tmp.add(df);
		}
		if (dt != null) {
			sql = sql + " and logTime<=?";
			tmp.add(dt);
		}
		Object[] params = new Object[tmp.size()];
		for (int i = 0; i < tmp.size(); i++) {
			params[i] = tmp.get(i);
		}

		sql += " order by logTime desc limit " + (page - 1) * pageSize + "," + pageSize;
		List<RuntimeLog> ret = this.db.queryForBeans(sql, RuntimeLog.class, params);
		this.setErrCode(db.getErrCode());
		return ret;
	}

	public RuntimeLog queryRuntimeLogDetail(long id) {
		String sql = "select * from sys_runtimelog where id=? and agentId=?";
		RuntimeLog logDetail = db.queryForBean(sql, RuntimeLog.class, new Object[]{id, CloudContextUtils.getAgentId()});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return logDetail;
	}

	//to log runtime error
	public static void logRuntime(String module, String msg){
		CLogDao obj = new CLogDao();
		obj.logRuntimeError(module, msg);
		obj.dispose();
	}
}
